/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.commons.helper;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: RegexHelper.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public class RegexHelper {

    /**
     * 查找字符串中是否有匹配正则表达式
     *
     * @param srcStr
     * @param regExp
     * @return
     */
    public static boolean regexMatcher(String srcStr, String regExp) {
        Pattern pattern = Pattern.compile(regExp);
        Matcher matcher = pattern.matcher(srcStr);
        return matcher.find();
    }

    /**
     * getWithRegExp
     * @param input
     * @param regExp
     * @return
     */
    public static String getWithRegExp(String input, String regExp) {
        Pattern r = Pattern.compile(regExp);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(input);
        if (m.find()) {
            return m.group(1);
        }

        return StringUtils.EMPTY;
    }

    /**
     * getWithRegExps
     * @param input
     * @param regExp
     * @return
     */
    public static List<String> getWithRegExps(String input, String regExp) {
        List<String> list = new ArrayList<>();
        Pattern r = Pattern.compile(regExp);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(input);
        while (m.find()) {
            list.add(m.group(1));
        }
        return list;
    }
}
